<?php
class NewsController extends BaseController{
    public function __construct($action, $urlValues){
        if(!Helper::isLogin()){
            $_SESSION['lastUrl']=Helper::currentPageURL();
            header('Location: ' . BASE_URL . 'account/login'); 
        }
        parent::__construct($action, $urlValues);
        require("models/news.php");
        $this->model = new NewsModel();
    }
    
    protected function index(){
        $this->view->output($this->model->index());
    }
    
    protected function create(){
        if(!empty($_POST)){
           
            $result = $this->model->create($_POST);
            if($result){
                $alert = array(
                    'class' => 'alert-success',
                    'message' => 'Add new user success'
                );
                $_SESSION['message']=$alert;
                header('Location: ' . BASE_URL . 'news/index');
            }
        }
        $this->view->output($this->model->create(''));
    }
    
    protected function update(){
        if(!empty($_POST)){
            $news_id = $_POST['news_id'];
            foreach($_POST as $col=>$value){
                $data[$col] = $value;
            }


            $img = $_FILES['img'];
            print_r($img);

            unset($data['news_id']);
            $result = $this->model->edit($news_id, $data);
            echo $result;
            if($result){
                $alert = array(
                    'class' => 'alert-success',
                    'message'=>'Update news successful'
                );
                $_SESSION['message'] = $alert;
                header('Location: ' . BASE_URL . 'News/index');
            }
        }
        $this->view->output($this->model->edit($news_id=$this->urlValues['id'],$data=''));
    }
    
    public function getlist(){
        $this->view->output($this->model->getList($this->urlValues['id'],"empty"));
    }
    
    protected function delete(){
        if(!empty($_POST)){
            $result = $this->model->delete('news', 'news_id IN(' . implode(',',$_POST['news_id']) .')');
        }else{
            $result = $this->model->delete('news', 'news_id=' . $this->urlValues['id']);
        }
        
        if($result){
            $alert = array(
                'class' => 'alert-success',
                'message' => 'Delete News success'
            );
            $_SESSION['message'] = $alert;
        }
        header('Location: ' . BASE_URL . 'News/index');
    }
    
    public function search(){
        if(isset($_POST)){
            
            //lay theo dieu kien la cate id duoc truyen khi minh chon ben selectbox kia
            $category_id = intval($_POST['category_id']);
            
            //dong $query = "SELECT news.*, categories.category_name $query = "SELECT news.*, categories.category_name 
            //la anh lay tat ca trong table news, va cot category_name ben category 
            $query = "SELECT news.*, categories.category_name 
                    from news 
                    left join categories 
                    on (categories.category_id  = news.category_id)";
            $query .= " where news.category_id = '{$category_id}'";
            $query .= ' order by news.news_id desc';
            //truyen $query nhung ji minh muon lay vao day. de Model, no thuc thi voi database
            $data = $this->model->selectMulti($query);
        } else {
            $data = array('error' => true);
        }
        echo json_encode($data);
        exit();
        }
    } 

?>